from PyQt5.QtWidgets import QDialog, QVBoxLayout, QFrame, QPushButton, QTextEdit, QHBoxLayout, QLabel
from datetime import datetime,timedelta
from func import view
from config import frame_config,Text_config

class Information_shops(QDialog):
    def __init__(self,parent=None):
        super().__init__()
        self.parent=parent
        self.client=self.parent.client
        self.shop_id=self.parent.shop_id
        self.shop_name=self.parent.shop_name
        print(self.shop_name,self.shop_id)
        self.resize(1200,900)
        text=f'{self.shop_name}数据详情'
        self.setWindowTitle(text)
        mainlayout=QVBoxLayout(self)
        self.initui(mainlayout)
    def initui(self,mainlayout):
        frame=view.create_scroll(mainlayout)
        layout1=QVBoxLayout(frame)

        layout2=QHBoxLayout()
        layout1.addLayout(layout2)

        frame = QFrame()
        layout2.addWidget(frame)
        frame.setFixedHeight(200)
        frame.setStyleSheet(frame_config.frame_style2)
        layout3 = QVBoxLayout(frame)
        text = f'{self.shop_name}今日销售额'
        label2 = QLabel(text)
        label2.setStyleSheet(Text_config.text_style2)
        layout3.addWidget(label2)
        now=datetime.now()
        zuo=now-timedelta(days=1)
        parser={
            'shop_id':self.shop_id,
            'data' :now
        }
        data1=self.client.get_Sale_for_day(parser)
        parser={
            'shop_id':self.shop_id,
            'data' :zuo
        }
        zuo_sum=self.client.get_Sale_for_day(parser).get('msg')
        sum=data1.get('msg')
        label=QLabel(str(sum))
        label.setStyleSheet(Text_config.text_style1)
        layout3.addWidget(label)


        frame = QFrame()
        layout2.addWidget(frame)
        frame.setFixedHeight(200)
        frame.setStyleSheet(frame_config.frame_style2)
        layout3 = QVBoxLayout(frame)
        text = f'{self.shop_name}相较昨日变化'
        label2 = QLabel(text)
        label2.setStyleSheet(Text_config.text_style2)
        layout3.addWidget(label2)

        sub=sum-zuo_sum
        label=QLabel(str(sub))
        label.setStyleSheet(Text_config.text_style1)
        layout3.addWidget(label)

        frame = QFrame()
        layout2.addWidget(frame)
        frame.setFixedHeight(200)
        frame.setStyleSheet(frame_config.frame_style2)
        layout3 = QVBoxLayout(frame)
        text = f'{self.shop_name}综合评分'
        label2 = QLabel(text)
        label2.setStyleSheet(Text_config.text_style2)
        layout3.addWidget(label2)
        parser={
            'shop_id':self.shop_id,
        }
        score=self.client.get_score(parser).get('msg')
        if score !=0:
            label=QLabel(str(score))
        else:
            label=QLabel('尚未评分')
        label.setStyleSheet(Text_config.text_style1)
        layout3.addWidget(label)

        frame = QFrame()
        layout2.addWidget(frame)
        frame.setFixedHeight(200)
        frame.setStyleSheet(frame_config.frame_style2)
        layout3 = QVBoxLayout(frame)
        text = f'{self.shop_name}累计销售额'
        label2 = QLabel(text)
        label2.setStyleSheet(Text_config.text_style2)
        layout3.addWidget(label2)
        parser={
            'shop_id':self.shop_id,
        }
        sale=self.client.get_Sale(parser).get('msg')
        label=QLabel(str(sale))
        label.setStyleSheet(Text_config.text_style1)
        layout3.addWidget(label)

        frame1=QFrame()
        frame1.setStyleSheet(frame_config.frame_style1)
        layout1.addWidget(frame1)
        layout2=QVBoxLayout(frame1)
        layout3 = QHBoxLayout()
        layout2.addLayout(layout3)
        btn_sub = QPushButton()
        btn_sub.setText('-')
        btn_sub.setFixedSize(40, 40)
        self.num = QTextEdit()
        self.num.setReadOnly(True)
        self.num.setText('1')
        self.num.setFixedSize(40, 40)
        btn_add = QPushButton()
        btn_add.setText('+')
        btn_add.setFixedSize(40, 40)
        layout3.addWidget(btn_sub)
        layout3.addWidget(self.num)
        layout3.addWidget(btn_add)
        btn_sub.clicked.connect(self.sub_num_clicked)
        btn_add.clicked.connect(self.add_num_clicked)
        btn_submit=QPushButton()
        btn_submit.setText('生成数据')
        btn_submit.setFixedSize(200, 40)
        layout3.addWidget(btn_submit)
        btn_submit.clicked.connect(self.submit)

        exit_btn=QPushButton()
        exit_btn.setText('返回')
        exit_btn.setFixedSize(200, 40)
        layout3.addWidget(exit_btn)
        exit_btn.clicked.connect(self.exit)

        frame2=QFrame()
        frame2.setStyleSheet(frame_config.frame_style3)
        layout2.addWidget(frame2)
        self.layout4=QVBoxLayout(frame2)

        frame = QFrame()
        layout2.addWidget(frame)
        layout3=QVBoxLayout(frame)
        parser={
            'shop_id': self.shop_id
        }
        self.parent.imgs_analysis(layout3,'http://127.0.0.1:8000/data_analysis_api/get_view_score',parser)

    def submit(self):
        view.clear_layout(self.layout4)
        parser={
            'nums' : int(self.num.toPlainText()),
            'shop_id': self.shop_id
        }
        print(parser)
        self.parent.imgs_analysis(self.layout4,'http://127.0.0.1:8000/data_analysis_api/shops_for_some_day',parser)
    #减少数量
    def sub_num_clicked(self):
        num=int(self.num.toPlainText())-1
        if num>=1:
            self.num.setText(str(num))

    # z增加数量
    def add_num_clicked(self):
        num = int(self.num.toPlainText()) + 1
        self.num.setText(str(num))
    def exit(self):
        self.close()
        self.accept()

    def accept(self):
        super().accept()
